#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300000;
long long a[maxn];
long long b[maxn];
long long c[maxn];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		int cnt = 0;
		long long ans = 0;
		scanf("%d",&n);
		for(int i = 0;i<n;i++)
		{
			scanf("%lld %lld",&a[i],&b[i]);
		}
		long long temp = 0;
		for(int i = 1;i<n;i++)
		{
			temp = a[i]-b[i-1];
			if(temp>0)
			{
				ans += temp;
				c[cnt] = b[i-1];
			}
			else c[cnt] = a[i];
			cnt++;
		}
		temp = a[0]-b[n-1];
		if(temp > 0)
		{
			ans += temp;
			c[cnt] = b[n-1];
		}
		else c[cnt] = a[0];
		cnt++;
		long long  mn = 1e12+1;
		for(int i = 0;i<cnt;i++)
		{
			if(c[i]<mn)
			{
				mn = c[i];
			}
		}
	//	printf("*****%d\n",sign);
		printf("%lld\n",ans+mn);
	}
} 
